﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Text;
using System.Web.UI.WebControls;

namespace dpruna.SharePoint
{
    public partial class LookupPage : LookupPageBase
    {
        //private const string ScriptKey = "LookupPageMultiSelect";
        //private bool AllowMultiSelect
        //{
        //    get
        //    {
        //        var allowMultiSelectString = this.Page.Request["MultiSelect"];
        //        var allowMultiSelect = false;
        //        if (!String.IsNullOrEmpty(allowMultiSelectString))
        //            allowMultiSelect = allowMultiSelectString == "1" ? true : false;

        //        return allowMultiSelect;
        //    }
        //}

        //protected void Page_Load(object sender, EventArgs e)
        //{
        //    if (!
        //        AllowMultiSelect)
        //    {
        //        SelectButtonBottom.Visible = false;
        //        SelectButtonTop.Visible = false;
        //        gridView.RowDataBound += new System.Web.UI.WebControls.GridViewRowEventHandler(gridView_RowDataBound);
        //        gridView.Columns[0].Visible = false;
        //    }
        //    else
        //    {
                
        //    }

        //    string list = this.Request["List"];
        //    Guid listId;
        //    if (Helpers.TryParseGuid(list, out listId))
        //    {
        //        SPList lookupList = SPContext.Current.Web.Lists[listId];
        //        SPQuery spQuery = new SPQuery();
        //        spQuery.ItemIdQuery = true;
        //        spQuery.QueryThrottleMode = SPQueryThrottleOption.Override;
        //        spQuery.RowLimit = 100;
        //        spQuery.ViewFields = String.Format("<FieldRef Name='ID'/><FieldRef Name='{0}'/>", this.Request["Field"]);
        //        spQuery.Query = @"<OrderBy Override='TRUE'><FieldRef Name='ID' /></OrderBy>";
        //        DataTable itemsDataSource = lookupList.GetItems(spQuery).GetDataTable();
        //        gridView.DataSource = itemsDataSource;
        //        gridView.DataKeyNames = new string[] { "ID" };
        //        gridView.DataBind();
        //    }
        //}

        //#region Registered Events
        //void gridView_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
        //{
        //    if (e.Row.RowType == System.Web.UI.WebControls.DataControlRowType.DataRow)
        //    {
        //        string rowKey = String.Empty;
        //        string rowText = String.Empty;
        //        DataRowView dataRV = e.Row.DataItem as DataRowView;
        //        if (dataRV != null && dataRV.Row != null)
        //        {
        //            rowKey = dataRV.Row["ID"].ToString();
        //            rowText = dataRV.Row[this.Request["Field"]].ToString();
        //            //e.Row.Attributes.Add("ondblclick",
        //            //    String.Format(@"SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, {0});return false;", String.Join(";#", new string[] { rowKey, rowText })));
        //            e.Row.Attributes.Add("ondblclick", String.Format("closePopup('{0}', '{1}', '{2}', '{3}');", this.Request["TextElem"], this.Request["ValElem"], rowKey, rowText));
        //        }

        //    }
        //} 
        //protected void SelectButton_Click(object sender, EventArgs e)
        //{
        //    StringBuilder rowKeyBuilder = new StringBuilder();
        //    StringBuilder rowTextBuilder = new StringBuilder();

        //    for (int i = 0; i < gridView.Rows.Count; i++)
        //    {
        //        GridViewRow row = gridView.Rows[i];
        //        bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;

        //        if (isChecked)
        //        {
        //            // Column 2 is the name column
        //            //
        //            // TODO: Column field to select should be passed as URL argument
        //            //
        //            DataRowView dataRV = gridView.Rows[i].DataItem as DataRowView;
        //            if (dataRV != null)
        //            {
        //                rowKeyBuilder.AppendFormat("{0};", dataRV["ID"]);
        //                rowTextBuilder.AppendFormat("{0};", dataRV["Title"]);
        //            }
        //        }
        //    }

        //    if (this.Page.ClientScript.IsClientScriptBlockRegistered(ScriptKey))
        //        this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), ScriptKey,
        //            String.Format("closePopup('{0}', '{1}', '{2}', '{3}');", this.Request["TextElem"], this.Request["ValElem"], rowKeyBuilder.ToString(), rowTextBuilder.ToString()));
        //}
        //#endregion
    }
}
